//
//  NSDateEx.m
//  hengyong
//
//  Created by 赛锋 张 on 12-5-24.
//  Copyright (c) 2012年 heng yong. All rights reserved.
//
//

#import "NSDateEx.h"


@implementation NSDate(DateEx)

+ (NSString *)getAgo:(int)ago
{	
	NSDate *begin = [NSDate dateWithTimeIntervalSince1970:ago];
    
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit| NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSCalendar *date = [NSCalendar currentCalendar];
    NSDateComponents *interval = [date components:unitFlags fromDate:begin toDate:[NSDate date] options:0];  
	
	int year  = [interval year];
	int month = [interval month];
	int week  = [interval week];
	int day   = [interval day];
	int hour  = [interval hour];
	int min   = [interval minute];
	int sec   = [interval second];
	
	NSString *time = @"";
	if (year > 0) 
	{
		time = [time stringByAppendingFormat:@"%d年前", year];
		return time;
	}
	else if (month > 0)
	{
		time = [time stringByAppendingFormat:@"%d个月前", month];
		return time;
	}
	else if (week > 0)
	{
		time = [time stringByAppendingFormat:@"%d周前", week];
		return time;
	}
	else if (day > 0)
	{
		time = [time stringByAppendingFormat:@"%d天前", day];
		return time;
	}
	else if (hour > 0)
	{
		time = [time stringByAppendingFormat:@"%d小时前", hour];
		return time;
	}
	else if (min > 0)
	{
		time = [time stringByAppendingFormat:@"%d分钟前", min];
		return time;
	}
	else if (sec > 0)
	{
		time = [time stringByAppendingFormat:@"%d秒前", sec];
		return time;
	}
	else
	{
		return @"刚刚";
	}

	return @"";
}

+ (NSString *)getMsTime:(int)standTime
{
    NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
    NSDate *begin = [NSDate dateWithTimeIntervalSince1970:standTime];
    NSDate *date = begin;
    [formatter setDateFormat:@"yyyy-MM-dd-HH:mm"];
    NSString *st = [formatter stringFromDate:date];
    return st;
}

+ (NSString *)getSSTime:(int)standTime
{
    NSString *stringNow = [NSString stringWithFormat:@"%.4f",[[NSDate date] timeIntervalSince1970]];
    int nowTe = [stringNow intValue];
    NSDateFormatter *formatterNow =[[[NSDateFormatter alloc] init] autorelease];
    NSDate *beginNow = [NSDate dateWithTimeIntervalSince1970:nowTe];
    NSDate *dateNow = beginNow;
    [formatterNow setDateFormat:@"yyyy"];
    NSString *stNow = [formatterNow stringFromDate:dateNow];
    
    NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
    NSDate *begin = [NSDate dateWithTimeIntervalSince1970:standTime];
    NSDate *date = begin;
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm"];
    NSString *st = [formatter stringFromDate:date];
    
    if ([st rangeOfString:stNow].location!=NSNotFound) 
    {
        st = [st substringWithRange:NSMakeRange(5,11)];
    }
    
    return st;

}

//当天的时候 前面显示 今天
+ (NSString *)getTodayTime:(int)standTime
{
    NSString *stringNowA = [NSString stringWithFormat:@"%.4f",[[NSDate date] timeIntervalSince1970]];
    int nowTeA = [stringNowA intValue];
    NSDateFormatter *formatterNowA =[[[NSDateFormatter alloc] init] autorelease];
    NSDate *beginNowA = [NSDate dateWithTimeIntervalSince1970:nowTeA];
    NSDate *dateNowA = beginNowA;
    [formatterNowA setDateFormat:@"yyyy"];
    NSString *stNowA = [formatterNowA stringFromDate:dateNowA];
    
    NSString *stringNow = [NSString stringWithFormat:@"%.4f",[[NSDate date] timeIntervalSince1970]];
    int nowTe = [stringNow intValue];
    NSDateFormatter *formatterNow =[[[NSDateFormatter alloc] init] autorelease];
    NSDate *beginNow = [NSDate dateWithTimeIntervalSince1970:nowTe];
    NSDate *dateNow = beginNow;
    [formatterNow setDateFormat:@"yyyy-MM-dd"];
    NSString *stNow = [formatterNow stringFromDate:dateNow];
    
    NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
    NSDate *begin = [NSDate dateWithTimeIntervalSince1970:standTime];
    NSDate *date = begin;
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *st = [formatter stringFromDate:date];
    
//    NSLog(@"stNow is %@,st is %@",stNow,st);
    if ([st rangeOfString:stNowA].location!=NSNotFound) 
    {
        //说明是今天
        if ([st rangeOfString:stNow].location!=NSNotFound) 
        {
            st = [st substringWithRange:NSMakeRange(5,11)];
            st = [st stringByReplacingCharactersInRange:NSMakeRange(0, 5) withString:@"今天"];
        }
        else 
        {
            st = [st substringWithRange:NSMakeRange(5,11)];
        }
    }
    else
    {
         st = [st substringWithRange:NSMakeRange(0, 10)];
    }
    
    return st;
    
}

-(NSString*) standardDayString
{
	NSDateFormatter* df = [[[NSDateFormatter alloc] init] autorelease];
	[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
	[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//[inputFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];2010-08-03 22:46:01
	return [df stringFromDate:self];
}

-(NSString*) standardDayString2
{
	NSDateFormatter* df = [[[NSDateFormatter alloc] init] autorelease];
	[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
	[df setDateFormat:@"yyyy-MM-dd"];
	return [df stringFromDate:self];
}

-(NSString*) standardDayString3
{
	NSDateFormatter* df = [[[NSDateFormatter alloc] init] autorelease];
	[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
	[df setDateFormat:@"yyyy-MM-dd HH:mm"];
	return [df stringFromDate:self];
}

@end
